package com.financing.controller.agent;
import java.util.Date;
import java.util.Map;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;

import com.financing.model.Data;
import com.financing.model.agent.AllotLog;
import com.financing.model.agent.Visit;
import com.financing.model.user.User;
import com.financing.service.agent.AllotLogService;
import com.financing.service.system.ParamService;
import com.financing.util.Json;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

/**
 *  拜访记录控制器
 * @since 2017-06-06
 * @author 无处不在的二叔
 */
public class AllotLogController extends Controller {
	private ParamService param = new ParamService();
	private AllotLogService allotLog = new AllotLogService(); 

	/**
	 * 拜访信息编辑 
	 */
	@RequiresRoles(logical=Logical.OR,value={"admin","agent_leader","agent_manager"})  
	public void index() {
		String agents = getPara("value","");
		if(param.findByName("ALLOT_AGENTS").setValue(agents).update()) {

			User creater = getAttr("user");
			AllotLog al = new AllotLog();
			al.setCreaterId(creater.getId());
			al.setCreaterRealName(creater.getRealName());
			al.setAgentsId(agents);
			String names = "";
			for(String s : agents.split(",")) {
				names += User.me.findById(s).getRealName()+",";
			}
			al.setAgentsRealName(names.length()>0 ? names.substring(0, names.length()-1) : names);
			al.setCreateTime(new Date());
			al.save();
			
		    renderJson(Json.ok("客服设置成功！"));
		} else {
		    renderJson(Json.error("客服设置失败！"));
		}
	}	
	
	/**
	 * 拜访列表
	 */
	public void list() { 
		Map<String,String> params = Data.flat(getParaMap());
		int page = getParaToInt("page",1);
		int size = getParaToInt("size",10);
		Page<Visit> p = allotLog.list(page,size,params);
		renderJson(Json.ok("数据获取成功！", Data.put("page",p)));
	}	
}
 